package com.sc.pattern.future;

import java.util.function.Consumer;

/**
 * Created  on 2018/1/7.
 *
 * @author sc
 */
public class FutureService {

    public <T> Future<T> submit(final FutureTask<T> task){
        AsyncFuture<T> asyncFuture = new AsyncFuture<>();
        new Thread(()->{
            T result = task.call();
            asyncFuture.done(result);
        }).start();
        return asyncFuture;
    }

    public <T> Future<T> submit(final FutureTask<T> task, final Consumer<T> consumer){
        AsyncFuture<T> asyncFuture = new AsyncFuture<>();
        new Thread(()->{
            T result = task.call();
            asyncFuture.done(result);
            consumer.accept(result);
        }).start();
        return asyncFuture;
    }
}
